package cn.hqm.hqmtechnology.service.impl;

import cn.hqm.hqmtechnology.dao.dto.SqlRecordDTO;
import cn.hqm.hqmtechnology.dao.mapper.SqlRecordMapper;
import cn.hqm.hqmtechnology.dao.po.SqlRecord;
import cn.hqm.hqmtechnology.dao.vo.SqlRecordVO;
import cn.hqm.hqmtechnology.service.SqlRecordService;
import cn.hqm.hqmtechnology.utils.IdGen;
import cn.hqm.hqmtechnology.utils.SqlSyntax;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SqlRecordServiceImpl extends ServiceImpl<SqlRecordMapper, SqlRecord> implements SqlRecordService {

    @Override
    public Object addSqlRecord(SqlRecordDTO sqlRecordDTO) {
        // 发起申请Sql执行记录并预审
        if (Objects.isNull(sqlRecordDTO.getSqlText()) || sqlRecordDTO.getSqlText().isEmpty()) {
            throw new RuntimeException("SqlText,请检查!");
        }
        if (Objects.isNull(sqlRecordDTO.getSqlHouse()) || sqlRecordDTO.getSqlHouse().isEmpty()) {
            throw new RuntimeException("SqlHouse,请检查!");
        }
        if (Objects.isNull(sqlRecordDTO.getEnv()) ) {
            throw new RuntimeException("env,请检查!");
        }
        if (Objects.isNull(sqlRecordDTO.getVersionId())) {
            throw new RuntimeException("versionId,请检查!");
        }
        // 校验sql 语法
        SqlSyntax.validateSyntax(sqlRecordDTO.getSqlText());

        SqlRecord sqlRecord = new SqlRecord();
        BeanUtils.copyProperties(sqlRecordDTO,sqlRecord);
        sqlRecord.setRecordId(IdGen.id());
        sqlRecord.setStatus(0);
        sqlRecord.setCreateTime(LocalDateTime.now());
        return this.save(sqlRecord);
    }

    @Override
    public Object pageSqlRecord(SqlRecordDTO sqlRecordDTO) {
        SqlRecord sqlRecord = new SqlRecord();
        BeanUtils.copyProperties(sqlRecordDTO,sqlRecord);
        Page<SqlRecord> sqlRecordPage = new Page<>();
        sqlRecordPage.setSize(sqlRecordDTO.getPageSize()).setCurrent(sqlRecordDTO.getPageNo());
        Page<SqlRecordVO> sqlRecordVOPage = new Page<>();
        Page<SqlRecord> page = this.page(sqlRecordPage, new LambdaQueryWrapper<SqlRecord>().setEntity(sqlRecord));
        ArrayList<SqlRecordVO> sqlRecordVOS = new ArrayList<>();
        for (SqlRecord record : page.getRecords()) {
            SqlRecordVO sqlRecordVO = new SqlRecordVO();
            BeanUtils.copyProperties(record,sqlRecordVO);
            sqlRecordVO.setId(record.getId().toString());
            sqlRecordVO.setRecordId(record.getRecordId().toString());
            sqlRecordVO.setVersionId(record.getVersionId().toString());
            sqlRecordVOS.add(sqlRecordVO);
        }
        sqlRecordVOPage.setRecords(sqlRecordVOS);
        sqlRecordVOPage.setSize(page.getSize());
        sqlRecordVOPage.setPages(page.getPages());
        sqlRecordVOPage.setCurrent(page.getCurrent());
        sqlRecordVOPage.setTotal(page.getTotal());
        return sqlRecordVOPage;
    }

    /**
     * 审核
     * @param sqlRecordDTO
     * @return
     */
    @Override
    public Object examine(SqlRecordDTO sqlRecordDTO) {
        // 判断用户传参
        if (Objects.isNull(sqlRecordDTO.getStatus()) || Objects.isNull(sqlRecordDTO.getId())) {
            throw new RuntimeException("审批状态和ID不能为空!");
        }

        SqlRecord one = this.getOne(new LambdaQueryWrapper<SqlRecord>().eq(SqlRecord::getId,sqlRecordDTO.getId()));

        if (Objects.isNull(one)) {
            throw new RuntimeException("数据不存在, 请检查!");
        }

        return this.update(
                new LambdaUpdateWrapper<SqlRecord>().eq(SqlRecord::getId,sqlRecordDTO.getId()).set(SqlRecord::getStatus,sqlRecordDTO.getStatus())
        );
    }

    @Override
    public Object generateData(SqlRecordDTO sqlRecordDTO) {
        // 查询版本sql
        Long versionId = sqlRecordDTO.getVersionId();
        List<SqlRecord> list = this.list(
                new LambdaQueryWrapper<SqlRecord>()
                        .eq(SqlRecord::getVersionId,versionId)
        );
        List<String> sqlList = list.stream().map(SqlRecord::getSqlText).collect(Collectors.toList());
        ArrayList<String> strings = new ArrayList<>();
        for (String item : sqlList) {
            char c = item.charAt(item.length() - 1);
            if (c != ';') {
                strings.add(item+";");
            } else {
                strings.add(item);
            }
        }
        return strings;
    }

    @Override
    public Object syncCheck(SqlRecordDTO sqlRecordDTO) {
        // 语法校验
        String sqlText = sqlRecordDTO.getSqlText();
        if (Objects.isNull(sqlText) || sqlText.isEmpty()) {
            throw new RuntimeException("提交参数异常,请检查!");
        }
        SqlSyntax.validateSyntax(sqlText);
        return true;
    }
}